package util;

import org.springframework.beans.factory.annotation.Autowired;
import pojo.Point24;
import service.Point24Service;

import java.util.List;

public class QueryDBGameHandler implements GameHandler {
    @Autowired
    Point24Service point24Service;
    private GameHandler nextHandler;

    @Override
    public void handleRequest() {
        if (solve()) return;

        if (nextHandler != null) {
            nextHandler.handleRequest();
        }

    }

    @Override
    public void setNextHandler(GameHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    /**
     * 数据库查询处理
     *
     * @return
     */
    @Override
    public boolean solve() {

        List<Integer> list = Game.getInstance().getDigits().subList(0, 4);
        List<Point24> result = point24Service.list(list.toString());
        if (result.isEmpty()) return false;

        Game.getInstance().getSolution().add(result.get(0).toString());
        return true;
    }


}
